import os
import sys
import time

import pygame

from constants.Constants import *
from role.Enemy import Enemy
from role.Explode import Explode
from role.Player import Player
from role.Wall import Wall


class TankBattle(object):
    enemys = pygame.sprite.Group()
    players = pygame.sprite.Group()
    direction = {UP: False, DOWN: False, LEFT: False, RIGHT: False}

    def __init__(self):
        pygame.init()
        self.screen = pygame.display.set_mode((GAME_WINDOW_WIDTH, GAME_WINDOW_HEIGHT), 0, 32)
        pygame.display.set_caption("坦克大战")

    def start(self):

        pygame.mixer.music.load("resources/start.wav")
        pygame.mixer.music.play()

        TankBattle.player1 = Player(self.screen)
        TankBattle.players.add(TankBattle.player1)
        TankBattle.player2 = Player(self.screen, False)
        TankBattle.players.add(TankBattle.player2)

        for e in range(0, 6):
            TankBattle.enemys.add(Enemy(self.screen))

        while True:
            self.screen.fill((0, 0, 0))
            self.draw_text(self.screen)
            self.draw_wall(self.screen)
            self.process_event()

            if len(TankBattle.players) == 0:
                print("enemy success")
            else:
                for player in TankBattle.players:
                    if player.play() == 1:
                        TankBattle.players.remove(player)

            if len(TankBattle.enemys) == 0:
                print("player success")
            else:
                for enemy in TankBattle.enemys:
                    if enemy.play() == 1:
                        TankBattle.enemys.remove(enemy)

            self.hit_enemy()
            self.hit_player()
            self.player_collide_enemy()

            pygame.display.update()
            time.sleep(0.01)

    def stop(self):
        sys.exit()

    def process_event(self):
        for event in pygame.event.get():
            if event.type == QUIT:
                self.stop()
            elif event.type == KEYDOWN:
                key = event.key
                print("按下键盘", event.key)
                if key == K_ESCAPE:
                    self.stop()
                elif DIRECTION.get(key):
                    TankBattle.direction[DIRECTION[key]] = True
                    TankBattle.player1.stop = False
                    TankBattle.player1.direction = DIRECTION[key]
                    TankBattle.player2.stop = False
                    TankBattle.player2.direction = DIRECTION[key]
                elif key == K_SPACE:
                    TankBattle.player1.fire()
                    TankBattle.player2.fire()
                elif (key == K_RETURN or key == K_KP_ENTER) and not TankBattle.player1.live:
                    TankBattle.player1 = Player(self.screen)
                    TankBattle.players.add(TankBattle.player1)
                elif key == K_EQUALS or key == K_KP_PLUS:
                    TankBattle.enemys.add(Enemy(self.screen))

            elif event.type == KEYUP:
                key = event.key
                print("放开键盘", event.key)
                if DIRECTION.get(key):
                    TankBattle.direction[DIRECTION[key]] = False

                    for d in TankBattle.direction:
                        if TankBattle.direction[d]:
                            return None

                    TankBattle.player1.stop = True
                    TankBattle.player2.stop = True
            elif event.type == MOUSEBUTTONDOWN:
                print("按下鼠标")

    def draw_text(self, screen):
        font = pygame.font.SysFont(os.path.abspath("resources/huawenkaiti.ttf"), 30)
        texts = [
            font.render("player count : %d" % len(TankBattle.players), True, (255, 0, 0)),
            font.render("enemy count : %d" % len(TankBattle.enemys), True, (255, 0, 0)),
            font.render("player1 missile count : %d" % len(TankBattle.player1.missiles), True, (255, 0, 0)),
            font.render("player2 missile count : %d" % len(TankBattle.player2.missiles), True, (255, 0, 0)),
            font.render("aaa", True, (255, 0, 0))
        ]
        for i, text in enumerate(texts, 0):
            screen.blit(text, (5, 5 + (30 * i)))

    def draw_wall(self, screen):
        wall = Wall(screen, 280, 280, 20, 280)
        wall.draw()
        wall.collide_wall()

        wall2 = Wall(screen, 280, 880, 20, 280)
        wall2.draw()
        wall2.collide_wall()

    def hit_enemy(self):
        for player in TankBattle.players:
            for missile in player.missiles:
                hit_list = pygame.sprite.spritecollide(missile, TankBattle.enemys, False)
                for hit in hit_list:
                    if hit.live:
                        hit.explode = Explode(hit)
                    hit.live = False
                    missile.live = False

    def hit_player(self):
        for enemy in TankBattle.enemys:
            for missile in enemy.missiles:
                hit_list = pygame.sprite.spritecollide(missile, TankBattle.players, False)
                for hit in hit_list:
                    if hit.live:
                        hit.explode = Explode(hit)
                    hit.live = False
                    missile.live = False

    def player_collide_enemy(self):
        for player in TankBattle.players:
            hit_list = pygame.sprite.spritecollide(player, TankBattle.enemys, False)
            for hit in hit_list:
                if hit.live:
                    hit.explode = Explode(hit)
                if player.live:
                    player.explode = Explode(hit)
                hit.live = False
                player.live = False
